package org.jeecg.modules.iot.model.modbus;

import lombok.Data;

// 定义 Modbus 参数类
@Data
public   class ModbusParams {
    public enum FunctionCode {
        READ_HOLDING_REGISTERS(3),
        WRITE_MULTIPLE_REGISTERS(16),
        WRITE_SINGLE_REGISTER(17);

        private final int code;

        FunctionCode(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }

    private FunctionCode functionCode;
    private int slaveAddress;
    private int startAddress;
    private int registerCount;
    private byte[] values;

    // Add this new method
   /* public void setValues(int... values) {
        // Calculate the size of the byte array
        this.values = new byte[values.length * 2]; // 2 bytes per integer

        // Convert each integer to two bytes and store in the array
        for (int i = 0; i < values.length; i++) {
            this.values[i * 2] = (byte) (values[i] >> 8);        // High byte
            this.values[i * 2 + 1] = (byte) (values[i] & 0xFF); // Low byte
        }
    }*/

    public void setValues(int... values) {
        // Calculate the size of the byte array
        this.values = new byte[values.length * 2]; // 2 bytes per integer

        // Convert each integer to two bytes and store in the array
        for (int i = 0; i < values.length; i++) {
            // 高位字节存储在低地址
            this.values[i * 2] = (byte) (values[i] >> 8);        // High byte
            // 低位字节存储在高地址
            this.values[i * 2 + 1] = (byte) (values[i] & 0xFF); // Low byte
        }
    }

    // 构造函数、Getter 和 Setter 方法
    // ...
}